/////////////////////////////////////////////////////////////////////////////
//
// File Name : bag.hpp
// File Type : 'CPP' Source File
// Purpose   : Base class, Manages lists of Objects
//
//
//  A bag is a collection of pointers to objects of any type.  A bag can
//  be used to maintain a "list" of elements, such that the list is not
//  guaranteed to be in any particular order.  Once a bag of items is
//  established, its ordering remains constant.
//
//
/////////////////////////////////////////////////////////////////////////////

#ifndef BAG_HPP
#define BAG_HPP
#include    <stdlib.h>

#include    "misc.h"
#include    "object.hpp"
#include    "memalloc.h"

#ifndef TRUE
#define TRUE      1
#define FALSE     0
#endif

class Bag : public Object
{
protected:
    int     nItems;
    void  **items;
    int     size;
    int     expandBy;

private:

public:
    isA() { return OBJTYPE_BAG; }
    TOOLDLL Bag( int bagSize = 10, int expansionSize = 10 );
    ~Bag() { MemFree(items); }
    TOOLDLL Bag( const Bag &aBag );

	// Overloaded new & delete
    void *  TOOLDLL operator new(size_t size);
    void   TOOLDLL operator delete(void *ptr);

    Bag & TOOLDLL operator = (const Bag &aBag);

    void TOOLDLL   dump(void)              { nItems = 0; }
    Bag& TOOLDLL   dumpInto( Bag &aBag );
    int     isEmpty(void)           { return (nItems == 0); }
    int     sizeOf()                { return size; }
    int     itemsIn()               { return nItems; }

    int isIn( void *anItem )
    {
        return ( locate( anItem ) >= 0 ) ? TRUE : FALSE;
    }

    int  TOOLDLL   locate( void *anItem );
    void * TOOLDLL put( void *anItem );
    void * TOOLDLL set( void *anItem, int index );
    void * TOOLDLL insert( void *anItem, int index);

    void * TOOLDLL get();
    void * TOOLDLL get( void *anItem );

    int TOOLDLL operator[](void *anItem)
                 { return locate(anItem); }

    void * TOOLDLL operator [](int i)
                { return ( (i >= 0 && i < nItems) ? items[i] : NULL ); }
    void * dataTable() { return items; }
};

#endif

